const { get } = require('../../../utils/request.js')
const EventBus = require('../../../utils/eventBus.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    triggered: false,
    timeMoney: 0,
    fixMoney: 0,
    totalInterest: 0,
    totalLendMoney: 0,
    showCard: true
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad({ unicode, owner }) {

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })

    EventBus.on('home.freshAllData', this.freshAllData)

    const interval = setInterval(() => {
      const token = wx.getStorageSync('token');
      if (token != undefined && token != '') {
        if (owner != undefined) {
          wx.setStorageSync('owner', owner);
        }
        if (unicode != undefined) {
          wx.setStorageSync('unicode', unicode);
        }
        this.freshData();
        clearInterval(interval);
      }
    }, 100);
  },

  freshAllData() {
    this.freshData();
  },

  freshData() {
    get('family/countChild', { unicode: wx.getStorageSync('unicode') }, (data) => { 
      setTimeout(() => {
        this.setData({
          triggered: false
        })
      }, 1000);

      if (data > 0) {
        this.setData({
          showCard: true,
        })
      } else {
        this.setData({
          showCard: false,
        })
      }

      const myComponent = this.selectComponent('#my-component');
      if (myComponent) {
        myComponent.freshData();
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  onShareAppMessage() {
    return {
      "title": `儿童微行`,
      "path": `pages/tabBar/summary/home`,
      "imageUrl": "/resoures/logo.png"
    }
  },
  onShareTimeline() {
    return {
      "title": `儿童微行`,
      "path": `pages/tabBar/summary/home`,
      "imageUrl": "/resoures/logo.png"
    }
  },
  downPullFresh() {
    this.freshData();
    const myComponent = this.selectComponent('#my-component');
    myComponent.freshData();
  },
  onChangeCard({ detail: { currentBalance, totalFixMoney, totalInterestIncome, totalLendMoney } }) {
    this.setData({
      timeMoney: currentBalance,
      fixMoney: totalFixMoney,
      totalInterest: totalInterestIncome,
      totalLendMoney: totalLendMoney
    })
  }

})